package com.xiaoyu.lanling.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;

import com.xiaoyu.lanling.R;

import java.util.Calendar;

public class AlertHoloDateDialog {

    private Context mContext;

    private AlertDialog mAlertDialog;
    private View mView;
    private TextView mDescView;
    private DatePicker mDatePicker;

    private static final int dateNullNum = -1;
    private int mMaxYear, mMaxMonth, mMaxDayOfMonth = dateNullNum;
    private int mMinYear, mMinMonth, mMinDayOfMonth = dateNullNum;
    private int mSelectedYear, mSelectedMonth, mSelectedDayOfMonth = dateNullNum;

    private OnDateResultListener mOnDateResultListener;

    public static AlertHoloDateDialog create(Context context) {
        return new AlertHoloDateDialog(context);
    }

    public AlertHoloDateDialog(Context context) {
        this.mContext = context;
        initView();
    }

    private void initView() {
        mView = LayoutInflater.from(mContext).inflate(R.layout.dialog_holo_date, new RelativeLayout(mContext));
        mDescView = mView.findViewById(R.id.data_picker_desc);
        mDatePicker = mView.findViewById(R.id.date_picker);
        mDatePicker.setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS);

        mAlertDialog = new AlertDialog.Builder(mContext)
                .setView(mView)
                .setPositiveButton("确认", (dialog, i) -> {
                    if (mOnDateResultListener != null) {
                        mOnDateResultListener.onDateResult(mSelectedYear, mSelectedMonth, mSelectedDayOfMonth);
                    }
                    dialog.dismiss();
                })
                .setNegativeButton("取消", (dialog, i) -> dialog.dismiss())
                .create();
    }

    private void initData() {
        mDatePicker.init(mSelectedYear, mSelectedMonth, mSelectedDayOfMonth, (view, year, monthOfYear, dayOfMonth) -> {
            this.mSelectedYear = year;
            this.mSelectedMonth = monthOfYear;
            this.mSelectedDayOfMonth = dayOfMonth;
        });
    }

    public AlertHoloDateDialog setTitle(String title) {
        mAlertDialog.setTitle(title);
        return this;
    }

    public AlertHoloDateDialog setDesc(String desc) {
        mDescView.setVisibility(View.VISIBLE);
        mDescView.setText(desc);
        return this;
    }

    public AlertHoloDateDialog setSelectedDate(int selectedYear, int selectedMonth, int selectedDayOfMonth) {
        this.mSelectedYear = selectedYear;
        this.mSelectedMonth = selectedMonth;
        this.mSelectedDayOfMonth = selectedDayOfMonth;

        if (mSelectedYear <= dateNullNum || mSelectedMonth <= dateNullNum || mSelectedDayOfMonth <= dateNullNum) {
            Calendar calendarSelected = Calendar.getInstance();
            mSelectedYear = calendarSelected.get(Calendar.YEAR);
            mSelectedMonth = calendarSelected.get(Calendar.MONTH);
            mSelectedDayOfMonth = calendarSelected.get(Calendar.DAY_OF_MONTH);
        }
        return this;
    }

    public AlertHoloDateDialog setMaxDate(int maxYear, int maxMonth, int maxDayOfMonth) {
        this.mMaxYear = maxYear;
        this.mMaxMonth = maxMonth;
        this.mMaxDayOfMonth = maxDayOfMonth;

        if (mMaxYear > dateNullNum && mMaxMonth > dateNullNum && mMaxDayOfMonth > dateNullNum) {
            Calendar calendarMax = Calendar.getInstance();
            calendarMax.set(mMaxYear, mMaxMonth, mMaxDayOfMonth);
            mDatePicker.setMaxDate(calendarMax.getTimeInMillis());
        }
        return this;
    }

    public AlertHoloDateDialog setMinDate(int minYear, int minMonth, int minDayOfMonth) {
        this.mMinYear = minYear;
        this.mMinMonth = minMonth;
        this.mMinDayOfMonth = minDayOfMonth;

        if (mMinYear > dateNullNum && mMinMonth > dateNullNum && mMinDayOfMonth > dateNullNum) {
            Calendar calendarMin = Calendar.getInstance();
            calendarMin.set(mMinYear, mMinMonth, mMinDayOfMonth);
            mDatePicker.setMinDate(calendarMin.getTimeInMillis());
        }
        return this;
    }

    public AlertHoloDateDialog show() {
        initData();
        mAlertDialog.show();
        return this;
    }

    public AlertHoloDateDialog dismiss() {
        mAlertDialog.dismiss();
        mAlertDialog = null;
        return this;
    }

    public AlertHoloDateDialog setOnDateResultListener(OnDateResultListener onDateResultListener) {
        this.mOnDateResultListener = onDateResultListener;
        return this;
    }

    public interface OnDateResultListener {
        void onDateResult(int year, int month, int dayOfMonth);
    }
}
